
namespace Core.GameObjects.States
{
    public class StateMachine
    {
        public BasicState CurrentState { get { return _CurrentState; } }
        public BasicState PreviousState { get { return _PreviousState; } }
        public BasicState GlobalState { get { return _GlobalState; } }

        GameObject _Owner;
        protected BasicState _CurrentState = null;
        protected BasicState _PreviousState = null;
        protected BasicState _GlobalState = null;

        public StateMachine(GameObject Owner)
        {
            _Owner = Owner;
        }

        public void Update()
        {
            //Execute the GlobalState
            if(_GlobalState != null) _GlobalState.Execute(_Owner);
            //Then Execute the CurrentState
            if(_CurrentState != null) _CurrentState.Execute(_Owner);
        }

        public void ChangeState(BasicState NewState)
        {
            //If the new state does not equal Null
            if (NewState == null)
                return;
            //Set Previous State equal to Current state
            _PreviousState = _CurrentState;
            //Run Exit routine for current state
            _CurrentState.Exit(_Owner);
            //Set Current state
            _CurrentState = NewState;
            //Run Enter routine for new state
            _CurrentState.Enter(_Owner);
        }

        public void RevertToPreviousState()
        {
            //Change to the previous state
            ChangeState(_PreviousState);
        }

        public bool IsInState(BasicState state)
        {
            return _CurrentState == state;
        }
    }
}
